<?php 
class shopModel extends CDBModelBase {
	public function getTabelName(){
		return "t_shop";
	}

	public function getPKID(){
		return "shop_id";
	}
	
	protected function getSequence()
	{
		return "SHOP_SEQUENCE";
	}
	
	/**
	 * 验证用户名是否重复
	 * @param string $username
	 * @param int $id
	 * @return boolean
	 */
	public function checkUserName($username,$id = 0)
    {
        $params = array(array(":user_name",$username,PDO::PARAM_STR));
        $where = "user_name=:user_name";
	    if($id)
        {
        	$params[] = array(":id",$id,PDO::PARAM_INT);
        	$where .= " and shop_id<>:id";
        }
		$count = $this->db->executeScalar(
				"select count(*) from ".$this->getTabelName()." where ".$where,$params);
		
		return $count>0?true:false;
    }
    
    /**
     * 商户登录
     * @param string $userName 用户名
     * @param string $pwd 密码
     */
    public function userLogin($userName, $pwd)
    {
    	$param = array(
    			array(":username",$userName,PDO::PARAM_STR),
    			array(":password",$pwd,PDO::PARAM_STR)
    	);
    
    	return $this->db->executeFetch("select shop_id,user_name,shop_name,first_login from ".$this->getTabelName()." where del_flag=0 and shop_status=1 and user_name=:username and user_pwd=:password",$param);
    }
    
	/**
     * 验证登录密码是否正确
     * @param int $shopID
     * @param string $pwd
     */
    public function checkUserPwd($shopID,$pwd)
    {
    	$param = array(
    			array(":shop_id",$shopID,PDO::PARAM_STR),
    			array(":user_pwd",$pwd,PDO::PARAM_STR)
    	);
    	
    	$count = $this->db->executeScalar("select count(0) from ".$this->getTabelName()." where shop_id=:shop_id and user_pwd=:user_pwd",$param);
    	
    	return $count>0?true:false;
    }
    
    /**
     * 验证交易密码是否正确
     * @param int $shopID
     * @param string $pwd
     */
    public function checkPayPwd($shopID,$pwd)
    {
    	$param = array(
    			array(":shop_id",$shopID,PDO::PARAM_STR),
    			array(":pay_pwd",$pwd,PDO::PARAM_STR)
    	);
    	
    	$count = $this->db->executeScalar("select count(0) from ".$this->getTabelName()." where del_flag=0 and shop_status=1 and shop_id=:shop_id and pay_pwd=:pay_pwd",$param);
    	
    	return $count>0?true:false;
    }
    
	/**
	 * 增加一条用户和社区对应关系
	 * @param int $userID
	 * @param int $communityID
	 */
	public function addShopCommunity($shopID, $communityID)
	{
		$sql = "select SHOP_COMMUNITY_SEQUENCE.NEXTVAL from dual";
		
		$sc_id = $this->db->executeScalar($sql);
		
		$param = array(
			array(":sc_id",$sc_id,PDO::PARAM_INT),
			array(":shop_id",$shopID,PDO::PARAM_INT),
			array(":community_id",$communityID,PDO::PARAM_INT)
		);
		
		$this->db->executeNonQuery("
		insert into t_shop_community(sc_id,shop_id,community_id)
		values(:sc_id, :shop_id, :community_id)"
		,$param);
		
		return $sc_id;
	}
	
	/**
	 * 删除用户有权限管理的社区
	 * @param int $userID
	 */
	public function delShopCommunity($shopID)
	{
		$param = array(
			array(":shop_id",$shopID,PDO::PARAM_INT)
		);
		
		return $this->db->executeNonQuery("
		delete from t_shop_community
		where shop_id=:shop_id",$param);
	}
	
	/**
	 * 获取用户有权限管理的社区
	 * @param int $userID
	 */
	public function getShopCommunity($shopID)
	{
		$param = array(
			array(":shop_id",$shopID,PDO::PARAM_INT)
		);
		
		return $this->db->executeDataSet("
		select uc.shop_id,uc.community_id,c.community_name, c.province, c.city, c.area from t_shop_community uc, t_community c
		where uc.community_id=c.community_id and shop_id=:shop_id"
		,$param);
	}
	
	public function getSumData($shopIDS)
	{
		return $this->db->executeDataSet(
			"select shop_id,sum(decode(cost_type,1,cost_money_t,0)) as add_sum,
			sum(decode(cost_type,2,cost_money_t,0)) as reduce_sum,
			sum(decode(cost_type,3,cost_money_t,0)) as cancel_sum 
			from T_INTEGRAL_CHANGE where shop_id in (".$shopIDS.") group by shop_id");
	}
	
	public function getShopProductByC($communityName)
	{
		$sql = "select community_id from t_community where del_flag=0 and community_name like '%".$communityName."%'";
		
		$cList =  $this->db->executeDataSet($sql);
		
		if(!$cList)
			return array();
		
		$cid = "";
		
		foreach($cList as $item)
		{
			if($cid)
				$cid .= ",";
			
			$cid .= $item["community_id"];
		}
		
		$sql = "select shop_id from t_shop_community where community_id in (".$cid.")";
		
		$cList =  $this->db->executeDataSet($sql);
		
		$sid = "";
		foreach ($cList as $v)
		{
			if($sid)
				$sid .= ",";
				
			$sid .= $v["shop_id"];
		}
		
		return $sid;
	}
	
	/**
	 * 商户数量
	 */
	public function getShopNum()
	{
		$count = $this->db->executeScalar(
		"select count(0) from ".$this->getTabelName()." where del_flag=0");
    	
    	return $count;
	}
	
	public function insertShopDesc($shopID,$content)
	{
		$params = array("shop_id"=>$shopID,"shop_desc"=>$content);
		return $this->db->insertLongData("insert into t_shop_desc(shop_id,shop_desc) values(:shop_id,EMPTY_CLOB())",$params,"shop_desc");
	}
	
	public function updateShopDesc($shopID,$content)
	{
		return $this->db->updateLongData("t_shop_desc","shop_id",$shopID,"shop_desc",$content);
	}
	
	public function getShopDesc($shopID)
	{
		return $this->db->selectLongDataSingle("select shop_desc from t_shop_desc where shop_id=:shop_id",array("shop_id"=>$shopID),"shop_desc");
	}
}
?>
